﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;

namespace DataFileApplication_WPF.Models
{
    [Serializable]
    public class DataGridStruct
    {
        //存储加入DataGrid的元素key值，用于判断是否重名
        public HashSet<string> KeysHashSet { get; set; }
        public ObservableCollection<DataGridItemStruct> DataGridItemStructs { get; set; }

        public DataGridStruct()
        {
            DataGridItemStructs = new ObservableCollection<DataGridItemStruct>();
            KeysHashSet = new HashSet<string>();



            //元素 集合有变化时，防止重名
            DataGridItemStructs.CollectionChanged += (sender, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (DataGridItemStruct newItem in e.NewItems)
                    {
                        // 订阅Key变化，实现Key变化时，判断是否重名
                        newItem.PropertyChanged += DataGridItemStruct_PropertyChanged;

                        // 初始化OriginalKey
                        if (string.IsNullOrEmpty(newItem.OriginalKey))
                            newItem.OriginalKey = newItem.Key;

                        string value = newItem.Key.ToString();
                        while (true)
                        {
                            if (KeysHashSet.Add(value))
                            {
                                newItem.Key = value;
                                //确认值后将OriginalKey赋值
                                newItem.OriginalKey = value;
                                break;
                            }
                            value = value + "_copy";
                        }
                    }
                }
                else if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (DataGridItemStruct oldItem in e.OldItems)
                    {
                        oldItem.PropertyChanged -= DataGridItemStruct_PropertyChanged;
                        KeysHashSet.Remove(oldItem.Key);
                    }
                }
            };
        }
        private void DataGridItemStruct_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(DataGridItemStruct.Key))
            {
                var item = sender as DataGridItemStruct;
                if (item == null) return;

                // 判断是否有重名
                int count = DataGridItemStructs.Count(x => x.Key == item.Key);
                if (count > 1)
                {
                    // 发现重名，可以弹窗、提示或处理
                    // 例如：
                    System.Windows.MessageBox.Show($"Key \"{item.Key}\" 已存在，请修改为唯一值。");

                    // 重命名失败时赋值
                    item.RenameState = false;

                    // 还原Key为原值
                    if (!string.IsNullOrEmpty(item.OriginalKey))
                         item.CanRaiseKeyState = false;
                        item.Key = item.OriginalKey;
                   item.CanRaiseKeyState = true;
                }
                else
                {
                    // 重命名成功时赋值
                    item.RenameState = true;
                    // 没有重名时，更新OriginalKey为当前Key
                   // item.OriginalKey = item.Key;
                }
            }
        }
        public DataGridStruct Clone()
        {
            // 创建一个新的 DataGridStruct 对象
            var cloned = new DataGridStruct
            {
                KeysHashSet = new HashSet<string>(this.KeysHashSet)
            };

            // 递归克隆 DataGridItemStructs 集合
            cloned.DataGridItemStructs = new ObservableCollection<DataGridItemStruct>();
            foreach (var item in this.DataGridItemStructs)
            {
                cloned.DataGridItemStructs.Add(item.Clone());
            }
            return cloned;
        }
    }

}
